package com.med.meddataintegrator.jwt.service.Impl;

import com.med.meddataintegrator.common.basic.exception.ServiceException;
import com.med.meddataintegrator.common.basic.response.JsonResult;
import com.med.meddataintegrator.common.basic.response.StatusCode;
import com.med.meddataintegrator.common.enums.UserRole;
import com.med.meddataintegrator.common.pojo.dos.PatientDo;
import com.med.meddataintegrator.common.pojo.dos.UserDo;
import com.med.meddataintegrator.common.pojo.dos.UserRoleDo;
import com.med.meddataintegrator.common.mapper.DoctorMapper;
import com.med.meddataintegrator.common.mapper.PatientDoMapper;
import com.med.meddataintegrator.common.mapper.UserDoMapper;
import com.med.meddataintegrator.common.mapper.UserRoleDoMapper;
import com.med.meddataintegrator.jwt.pojo.dto.PatientRegisterParams;
import com.med.meddataintegrator.jwt.pojo.dto.RegisterUserParams;
import com.med.meddataintegrator.jwt.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;



@Service
@Slf4j
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDoMapper userDoMapper;
    @Autowired
    private PatientDoMapper patientDoMapper;
    @Autowired
    private DoctorMapper doctorMapper;
    @Autowired
    private UserRoleDoMapper userRoleDoMapper;
    // 密码加密器
    @Autowired
    private PasswordEncoder passwordEncoder;
    @Override
    @Transactional
    public JsonResult register(RegisterUserParams registerUserParams) {

        // 在user表中查找是否存在
        UserDo userDo = userDoMapper.selectByUserName(registerUserParams.getUsername());

        if (userDo != null){
            throw new ServiceException(StatusCode.USER_EXIST);
        }

        String password = registerUserParams.getPassword();
        String encoder = passwordEncoder.encode(password);
        log.info("加密前：{}，加密后：{}", password, encoder);
        // 转Do
        UserDo user = UserDo.builder().password(encoder)
                .username(registerUserParams.getUsername())
                .role(registerUserParams.getRole())
                .build();

       userDoMapper.insert(user);



        Integer role = registerUserParams.getRole();
        if (role == UserRole.PATIENT.getRole()){

            // 角色关联表中插入数据
            UserRoleDo userRoleDo = UserRoleDo.builder()
                    .userId(user.getId())
                    .roleId(UserRole.PATIENT.getRole())
                    .build();
            userRoleDoMapper.insert(userRoleDo);

            // 更新对应患者表中的数据
            PatientRegisterParams patientRegisterParams = (PatientRegisterParams) registerUserParams;
            PatientDo patientDo = PatientDo.builder()
                    .userId(user.getId()) // 拿到新增加的id
                    .age(patientRegisterParams.getAge())
                    .certId(patientRegisterParams.getCertId())
                    .sex(patientRegisterParams.getSex())
                    .address(patientRegisterParams.getAddress())
                    .name(patientRegisterParams.getName())
                    .build();
            patientDoMapper.insert(patientDo);
        } else {
            // TODO  此处进行医生的注册
        }
        return JsonResult.ok();
    }
}
